/* 
 * Copyright (c) 2009, Giora Kosoi giorak@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Giora Kosoi ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Giora Kosoi BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sparrow/variant_int64.h>
#include <sparrow/errors.h>

VariantInt64::VariantInt64(int64_t value)
    :   Variant(VT_INT64),
        value_(value)
{
}

VariantInt64::VariantInt64(const std::string &value)
    :   Variant(VT_INT64)
{
    str(value);
}

VariantInt64::VariantInt64(const Octets &value)
    :   Variant(VT_INT64)
{
    octets(value);
}

VariantInt64::VariantInt64(const Octets::const_iterator &begin, const Octets::const_iterator &end)
    :   Variant(VT_INT64)
{
    octets(begin, end);
}

VariantInt64::~VariantInt64()
{
}

int64_t VariantInt64::int64() const
{
    return value_;
}

void VariantInt64::int64(int64_t value)
{
    value_ = value;
    s_.clear();
    o_.clear();
}

uint64_t VariantInt64::uint64() const
{
    return value_;
}

void VariantInt64::uint64(uint64_t value)
{
    value_ = value;
    s_.clear();
    o_.clear();
}

const Octets &VariantInt64::octets() const
{
    if(o_.empty())
    {
        uint64_t big = bswap_64(value_);
        o_.insert(o_.begin(), (uint8_t *)&big, (uint8_t *)&big + sizeof(big));
    }
    return o_; 
}

void VariantInt64::octets(const uint8_t *value, size_t value_len)
{
    if(value == NULL || value_len != sizeof(value_))
    {
        throw_error<std::runtime_error>(__FILE__, __LINE__, "value cannot be converted to int64_t");
    }
    value_ = bswap_64(*(uint64_t *)value);
    s_.clear();
    o_.clear();
}

void VariantInt64::octets(const Octets::const_iterator &begin, const Octets::const_iterator &end)
{
    octets(&(*begin), end - begin);
}

void VariantInt64::octets(const Octets &value)
{
    octets(&value.at(0), value.size());
}

const std::string &VariantInt64::str() const
{
    if(s_.empty())
    {
        char buff[32];
        size_t size = sprintf(buff, "%lld", value_);
        s_.assign(buff, size);
    }
    return s_;
}

const std::string &VariantInt64::pstr() const
{
    return str();
}

void VariantInt64::str(const std::string &value)
{
    char* endptr = NULL;    
    errno = 0;
    int64_t new_value = strtoll(value.c_str(), &endptr, 10);
    if((endptr != 0 && *endptr != '\0') || errno == ERANGE) 
    {
        throw_error<std::runtime_error>(__FILE__, __LINE__, "value [%s] cannot be converted to int64_t", value.c_str());
    }
    value_ = new_value;
    s_.clear();
    o_.clear();
}

Variant *VariantInt64::clone() const
{
    return new VariantInt64(value_);
}



